package com.yy.yycompnent.slide
{
	import com.yy.base.App;
	
	import flash.display.DisplayObject;
	import flash.display.MovieClip;
	import flash.events.Event;
	import flash.events.EventDispatcher;
	import flash.events.MouseEvent;
	import flash.events.TimerEvent;
	import flash.utils.Timer;

	/**
	 *滚动条控制器 
	 * @author YY
	 * 
	 */
	public class ScrollBar extends EventDispatcher
	{
		protected const DELAY_TIME:int = 500;
		protected const REPEAT_TIME:int = 100; 
		protected const UP:String = "up";
		protected const DOWN:String = "down";
		
		protected var _autoHide:Boolean = true;//是否自动隐藏
		protected var _upButton:DisplayObject;//向上按钮
		protected var _downButton:DisplayObject;//向下按钮
		protected var _slideButton:MovieClip;//向下按钮
		protected var _scrollSlider:ScrollSlider;//滚动条
		protected var _orientation:String;//滚动方向
		protected var _lineSize:int = 1;//点击上下按钮 改变scrollslider的值
		protected var _delayTimer:Timer;
		protected var _repeatTimer:Timer;
		protected var _direction:String;//滑块正在移动的方向
		protected var _shouldRepeat:Boolean = false;
		public function ScrollBar(upView:DisplayObject, downView:DisplayObject, slideBar:MovieClip, defaultHandle:Function=null,dir:String = Slide.VERTICAL)
		{
			_upButton = upView;
			_downButton = downView;
			_slideButton = slideBar;
			_orientation = dir;
			if(Boolean(defaultHandle)) {
				addEventListener(Event.CHANGE, defaultHandle);
			}
			init();
		}
		
		/**
		 *初始化控制器 
		 * 
		 */
		private function init():void {
			_scrollSlider = new ScrollSlider(_slideButton.back, _slideButton.handle, _orientation, changeFunc);
			addEvent();
			draw();
		}
		/**
		 *添加上下按钮的侦听 
		 * 
		 */
		private function addEvent():void {
			_upButton.addEventListener(MouseEvent.MOUSE_DOWN, clickUpFunc);
			_downButton.addEventListener(MouseEvent.MOUSE_DOWN, clickDownFunc);
			_delayTimer = new Timer(DELAY_TIME, 1);
			_delayTimer.addEventListener(TimerEvent.TIMER_COMPLETE, onDelayComplete);
			_repeatTimer = new Timer(REPEAT_TIME);
			_repeatTimer.addEventListener(TimerEvent.TIMER, onRepeat);
		}
		
		/**
		 *启动重复上下操作延时 
		 * @param e
		 * 
		 */
		private function onDelayComplete(e:Event):void {
			if(_shouldRepeat)
			{
				_repeatTimer.start();
			}
		}
		/**
		 *重复上下操作 
		 * @param event
		 * 
		 */
		protected function onRepeat(event:TimerEvent):void
		{
			if(_direction == UP)
			{
				goUp();
			}
			else
			{
				goDown();
			}
		}
		
		/**
		 *移除上下按钮的侦听 
		 * 
		 */
		private function removeEvent():void {
			_upButton.removeEventListener(MouseEvent.CLICK, clickUpFunc);
			_downButton.removeEventListener(MouseEvent.CLICK, clickDownFunc);
		}
		
		/**
		 *点击向上的按钮 
		 * @param e
		 * 
		 */
		private function clickUpFunc(e:Event):void {
			goUp();
			_shouldRepeat = true;
			_direction = UP;
			_delayTimer.start();
			App.appStage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}
		/**
		 *向上移动 
		 * 
		 */
		private function goUp():void {
			_scrollSlider.value -= _lineSize;
		}
		/**
		 *点击向下的按钮 
		 * @param e
		 * 
		 */
		private function clickDownFunc(e:Event):void {
			goDown();
			_direction = DOWN;
			_shouldRepeat = true;
			_delayTimer.start();
			App.appStage.addEventListener(MouseEvent.MOUSE_UP, onMouseGoUp);
		}
		protected function onMouseGoUp(event:MouseEvent):void
		{
			_delayTimer.stop();
			_repeatTimer.stop();
			_shouldRepeat = false;
		}
		/**
		 *向下移动一行 
		 * 
		 */
		private function goDown():void {
			_scrollSlider.value += _lineSize;
		}
		
		private function changeFunc(e:Event):void {
			dispatchEvent(new Event(Event.CHANGE));
		}
		
		
		//////////////////////////
		//////public function
		//////////////////////////
		public function draw():void {
			if(_autoHide) {
				var boo:Boolean = _scrollSlider.thumbPercent < 1;
//				_upButton.visible = boo;
//				_downButton.visible = boo;
				_scrollSlider.show(boo);
			}else {
				_upButton.visible = true;
				_downButton.visible = true;
				_scrollSlider.show(true);
			}
		}
		/**
		 * Sets the percentage of the size of the thumb button.
		 */
		public function setThumbPercent(value:Number):void
		{
			_scrollSlider.setThumbPercent(value);
			draw();
		}
		
		/**
		 *设置滚动条参数 
		 * @param min
		 * @param max
		 * @param value
		 * 
		 */
		public function setSliderParams(min:Number, max:Number, value:Number):void
		{
			_scrollSlider.setSliderParams(min, max, value);
		}
		//////////////////////////
		///////setter getter
		//////////////////////////
		/**
		 * Sets / gets the current value of this scroll bar.
		 */
		public function set value(v:Number):void
		{
			_scrollSlider.value = v;
		}
		public function get value():Number
		{
			return _scrollSlider.value;
		}
		
		public function set autoHide(value:Boolean):void
		{
			_autoHide = value;
			_scrollSlider.draw();
		}
		public function get autoHide():Boolean
		{
			return _autoHide;
		}
	}
}